import { Document } from "mongodb";
import { mongo } from "../mogoConfig";
import { it } from "node:test";

interface Weather extends Document {
    ts: Date,
    type: String
}

const run = async () => {
    const dataTB = mongo.db("sample_weatherdata").collection<Weather>("data")

    const agg1 = dataTB.aggregate([{
        $match: {
            ts: {
                $lt: new Date(),
                $gte: new Date(1980, 0, 1)
            }
        },
    },
    {
        $group: {
            _id: "$type",
            count: { $sum: 1 }
        }
    }
    ])

    console.log("for循环方式------------------------")
    for await (const item of agg1) {
        console.log(JSON.stringify(item, null, 2))
    }

    console.log("while循环方式------------------------")
    await agg1.rewind()
    while (await agg1.hasNext()) {
        console.log(JSON.stringify(await agg1.next(), null, 2))
    }

    console.log("返回所有------------------------")
    await agg1.rewind();
    (await agg1.toArray()).forEach(item => {
        console.log(JSON.stringify(item, null, 2))
    })

}






run()
    .catch(error => {
        console.log(error)
    })
    .finally(() => {
        console.log("执行完成")
        mongo.close()
    })